<template>
  <div class="left-box">
    <div class="right-info-repair box-shadow" v-for="(repair,key) in data.parts" :key="key">
      <div class="right-info-repair-name">维修部件{{key + 1}}</div>
      <div v-for="(part,index) in repair.warehouses" :key="index">
        <div class="part-top">
          <div class="right-info-part">
            <div class="part">
              <span v-if="part.type == 1">急件：</span>
              <span v-else>配件：</span>
              <span style="width: 150px;display: inline-block;">{{part.name}}</span>
              <div 
                class="right-info-part-show" 
                @click="slideShow('index' + key + '_' + index,$event)">
                <span style="margin-right: 10px;">点击展开修改</span>
                <img width="14" :src="img" />
              </div>
            </div>
          </div>
        </div>
        <div class="part-show" :id="'index' + key + '_' + index">
          <table cellspacing="0">
            <tr 
              v-if="part.fakeStatus == 0" 
              class="border">
              <td align="left"><span class="star-2">*</span><span style="color: rgba(250, 140, 22, 1);">供应商名称</span></td>
              <td><input v-model="part.supplier" class="part-input" placeholder="点击填写名称" type="text"/></td>
            </tr>
            <tr class="border">
              <td>
                <span class="star-2">*</span>
                <span v-if="part.type == 1">急件名称</span>
                <span v-else>配件名称</span>
              </td>
              <td><input v-model="part.name" class="part-input" placeholder="点击填写急件名" type="text"/></td>
            </tr>
            <tr 
              v-if="part.fakeStatus == 0"
              class="border">
              <td><span class="star-2">*</span>采购单价(元)</td>
              <td><input v-model="part.inPrice" class="part-input" placeholder="0.00" type="text"/></td>
            </tr>
            <tr class="border">
              <td><span class="star-2">*</span>销售单价(元)</td>
              <td><input v-model="part.outPrice" class="part-input" placeholder="0.00" type="text"/></td>
            </tr>
            <tr class="border">
              <td><span class="star-2">*</span>数量</td>
              <td><input v-model="part.count" class="part-input" placeholder="填写数量" type="text"/></td>
            </tr>
            <tr class="border">
              <td><span class="star-2">*</span>是否空出</td>
              <td>
                <a v-if="part.fakeStatus == 0" @click="isNull(key,index)" href="javascript:">
                  <img style="float: right;position: relative;top: 5px;" width="60" src="../../../assets/images/off.svg"/>
                </a>
                <a v-if="part.fakeStatus == 1" @click="isNull(key,index)" href="javascript:">
                  <img style="float: right;position: relative;top: 0px;" width="53" src="../../../assets/images/open.svg"/>
                </a>
              </td>
            </tr>
            <tr 
              v-if="part.fakeStatus == 0 || part.type == 0"
              style="height: 140px;">
              <td style="padding-top: 16px;border: 0;" colspan="2" >
                <!-- 三个照片框 -->
                <div style="height: 140px;">
                  <div class="upload-img">
                    <div><span class="star-2">*</span><span>旧件照片</span></div>
                    <div class="img-box" style="margin-top: 16px;">
                      <div v-for="(im,index) in part.wImgs11" :key="index">
                        <el-image
                          v-if="index < 4"
                          style="width: 35px;height: 35px;"
                          :src="im"
                          :preview-src-list="part.wImgs11"
                          fit="cover"
                        ></el-image>
                      </div>
                    </div>
                    <div>
                      <img 
                        @click="addImg(key,index,11)"
                        style="width: 90px;height: 29px;cursor: pointer;position: relative;top: -29px;" 
                        src="../../../assets/images/orderslogo16.png" />
                    </div>
                  </div>
                  <div class="upload-img">
                    <div><span class="star-2">*</span><span>新件照片</span></div>
                    <div class="img-box" style="margin-top: 16px;">
                      <div v-for="(im,index) in part.wImgs13" :key="index">
                        <el-image
                          v-if="index < 4"
                          style="width: 35px;height: 35px;"
                          :src="im"
                          :preview-src-list="part.wImgs13"
                          fit="cover"
                        ></el-image>
                      </div>
                    </div>
                    
                    <div>
                      <img 
                        @click="addImg(key,index,13)"
                        style="width: 90px;height: 29px;cursor: pointer;position: relative;top: -29px;" 
                        src="../../../assets/images/orderslogo16.png" />
                    </div>
                  </div>
                  <div v-if="part.type == 1" class="upload-img">
                    <div><span class="star-2">*</span><span>单据照片</span></div>
                    <div class="img-box" style="margin-top: 16px;">
                      <div v-for="(im,index) in part.wImgs14" :key="index">
                        <el-image
                          v-if="index < 4"
                          style="width: 35px;height: 35px;"
                          :src="im"
                          :preview-src-list="part.wImgs14"
                          fit="cover"
                        ></el-image>
                      </div>
                    </div>
                    
                    <div>
                      <img 
                        @click="addImg(key,index,14)"
                        style="width: 90px;height: 29px;cursor: pointer;position: relative;top: -29px;" 
                        src="../../../assets/images/orderslogo16.png" />
                    </div>
                  </div>
                  <!-- 照片框 -->
                  
                </div>
                
                
              </td>
            </tr>
            <tr>
              <td style="padding-top: 16px;border: 0;" colspan="2">
                <!-- 两个按钮 -->
                <div class="buttons">
                  <button type="button" :disabled="saveBtn" class="btn btn-1" @click="savePart(key,index)">
                    <span><img  class="btn-img" src="../../../assets/images/save.svg" /></span>
                    <span style="margin-left: 5px;color: rgba(250, 140, 22, 1);">保存急件</span>
                  </button>
                  <el-popconfirm
                    title="您确定要删除吗？"
                    confirm-button-type="danger"
                    @confirm="deletePart(key,index)"
                  >
                    <button slot="reference" type="button" class="btn btn-2">
                      <img class="btn-img"  src="../../../assets/images/delete_btn.svg" />
                    </button>
                  </el-popconfirm>
                </div>
              </td>
            </tr>
          </table>
        </div>
      </div>
      <div style="display: flex;justify-content: space-around;">
        <button 
          @click="addPart(key)"
          type="button" 
          class="btn btn-3">
          <span>+</span>
          <span style="margin-left: 5px;">配件出库</span>
        </button>
        <button 
          @click="addjijian(key)"
          type="button" 
          class="btn btn-4">
          <span><img class="btn-img" src="../../../assets/images/jiaji.svg" /></span>
          <span style="margin-left: 5px;">添加急件</span>
        </button>
      </div>
    </div>
    
  </div>

</template>

<script>
  import $ from 'jquery'
  import {addParts,deleteParts} from '@/api/workspace/work_order'
  export default {
    props:{
      data:Object,
      id:String
    },
    data() {
      return {
        img:'./images/top.svg',
        saveBtn:false
      }
    },
    created() {
    },
    mounted() {
        // console.log(this.data.parts)
    },
    methods:{
      
      slideShow:function(key,e){
        $('#' + key).slideToggle(200);
        var text = $(e.currentTarget).children().eq(0);
        var src = $(e.currentTarget).children().eq(1);
        if(text.text() == '点击展开修改'){
          text.text('点击收起');
          src.attr('src','./images/bottom.svg');
        }else {
          text.text('点击展开修改');
          src.attr('src','./images/top.svg');
        }
      },
      isNull:function(key,index){
        // console.log(key,index)
        var data = this.data.parts[key];
        // console.log(this.data)
        var info = data.warehouses[index];
        info.fakeStatus = Math.abs(info.fakeStatus - 1);
      },
      addjijian:function(key){
        // var that = this;
        var info = this.data.parts[key];
        var da = {
          count: 0,
          fakeStatus: 0,
          inPrice: "",
          name: "",
          outPrice: "",
          partId: info.id,
          settlementId: info.settlementId,
          type: 1,
          supplier:""
        };
        info.warehouses.push(da);
        setTimeout(function(){
          $('#index' + key + '_' + (info.warehouses.length - 1)).prev().find('.right-info-part-show').click();
        },10);
        


      },
      addPart:function() {

      },
      deletePart:function(key,index){
        console.log(this.data.parts[key].warehouses[index].id);
        var that = this;
        deleteParts({id:that.data.parts[key].warehouses[index].id}).then(response => {
          var _data = response.data;
          console.log(_data)
          if(_data.status == 200){
            
            that.data.parts[key].warehouses.splice(index,1);
            this.$notify({
              title:"成功",
              message:"删除成功",
              type:"success",
              offset:550,
              duration:2000
            })
          }else {
            this.$notify({
              title:"提示",
              message:"发生错误",
              type:"error",
              offset:550,
              duration:2000
            })
            
          }
        }).catch(error => {
          alert('网络错误');
          console.log(error)
        })
      },
      savePart:function(key,index){
        var that = this;
        var info = that.data.parts[key].warehouses[index];

        addParts(info).then(response => {
          var _data = response.data;
          if(_data.status == 200){
            that.$notify({
              title:"提示",
              message:"保存成功",
              type:"success",
              offset:550,
              duration:2000
            })
            that.saveBtn = true;
            setTimeout(function(){
              that.saveBtn = false;
              // $('#index' + key + '_' + index).prev().find('.right-info-part-show').click();
              that.$emit('refresh');
            },1500);
          }else {
            that.$message({
              message:'请求失败',
              showClose:true,
              offset:'100',
              type:'error'
            })
          }
        }).catch(error => {
          alert('请求错误');
          console.log(error)
        })

      },
      addImg:function(key,index,type) {
        if(this.data.parts[key].warehouses[index].id == null) {
          this.$notify({
            title:"提示",
            message:"请先保存再进行添加照片",
            type:"warning",
            offset:550,
            duration:2000
          })
        }else {
          this.$emit('addWareImg',key,index,type);
        }
        
      }
    }
  }
</script>

<style lang="css" scoped>  
  .left-box {
    height: 725px;		
    border-radius: 8px;
    padding-left: 3px;
    padding-right: 3px;
    padding-top: 10px;
    margin-top: -10px;		
    margin-left: -4px;
    overflow-y: auto;
    width: 360px;
  }
  .right-info-repair{
    background-color: rgba(255, 255, 255, 1);
    border-radius: 8px;
    overflow: hidden;
    padding-bottom: 15px;
    margin-bottom: 10px;
  }
  .right-info-repair-name{  
    overflow-wrap: break-word;
    color: rgba(250, 140, 22, 1);
    background-color: rgba(246, 247, 248, 1);
    font-size: 16px;
    font-family: PingFangSC-Medium;
    white-space: nowrap;
    line-height: 22px;
    height: 55px;
    text-indent: 12px;
    line-height: 55px;
  }
  .right-info-part{
    height: 40px;
    overflow-wrap: break-word;
    color: rgba(51, 51, 51, 1);
    font-size: 14px;
    white-space: nowrap;
    line-height: 40px;
    margin-top: 8px;
  }
  .part{
    background-color: rgba(246, 247, 248, 1);
    padding: 0 12px;
  }
  .part>span{
    overflow-wrap: break-word;
    color: rgba(51, 51, 51, 1);
    font-size: 14px;
    white-space: nowrap;
  }
  .part-top{
    /* margin-bottom: 15px; */
  }
  .part-show{
    padding: 12px;
    display: none;
  }
  .star-2{
    color: rgba(250, 140, 22, 1);
    margin-right: 5px;
  }
  .part-show table{
    width: 100%;
  }
  .part-show table tr{
    height: 55px;
    
    font-size: 14px;
    color: rgba(51, 51, 51, 1);
    line-height: 20px;
  }
  table td {
    border-bottom: 1px solid rgba(220, 220, 220, 1);
  }
  .part-input{
    border: none;
    text-align: right;
    outline: medium;
    width: 100%;
    overflow-wrap: break-word;
    color: rgba(51, 51, 51, 1);
    font-size: 14px;
    white-space: nowrap;
    line-height: 20px;
  }
  .right-info-part-show{
    overflow-wrap: break-word;
    color: rgba(170, 170, 170, 1);
    font-size: 12px;
    white-space: nowrap;
    float: right;
    cursor: pointer;
  }
  .upload-img{
    width: 90px;
    height: 90px;
    float: left;
    /* text-align: center; */
    margin-right: 15px;
  }
  .img-box {
    background: url(../../../assets/images/imgs_box.svg) 0px 0px no-repeat;
    height: 90px;
    overflow: hidden;
  }
  .img-box > div {
    width: 35px;
    height: 35px;
    float: left;
    margin-left: 7px;
    margin-top: 6px;
    cursor: pointer;
    border-radius: 3px;
    overflow: hidden;
  }
  .buttons{
    width: 100%;
  }
  .btn {
    border-radius: 8px;
    font-size: 16px;
    overflow-wrap: break-word;
    white-space: nowrap;
    cursor: pointer;
    height: 45px;
  }
  .btn-img{
    width: 16px;
    position: relative;
    top: 3px;
  }
  .btn-1{
    border: 1px dashed rgba(250, 140, 22, 1);
    background-color: rgba(250, 140, 22, 0.1);
    width: 250px;
    line-height: 45px;
  }
  .btn-2{
    width: 50px;
    border: 1px dashed rgba(255, 59, 48, 1);
    background-color: rgba(255, 59, 48, 0.1);
    margin-left: 10px;
  }
  .btn-3{
    border: 1px dashed rgba(153, 153, 153, 1);
    width: 150px;
    margin-top: 15px;
    color: rgba(250, 140, 22, 1);
    margin-left: 12px;
  }
  .btn-4{
    color: rgba(224, 32, 32, 1);
    border: 1px dashed rgba(153, 153, 153, 1);
    width: 150px;
    margin-left: 10px;
    margin-right: 20px;
    margin-top: 15px;
  }

</style>
